package com.zpkj.portal.service.redis.impl;
//package com.zpkj.portal.service.redis;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.dao.DataAccessException;
//import org.springframework.data.redis.connection.RedisConnection;
//import org.springframework.data.redis.core.RedisCallback;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.stereotype.Service;
//
//import java.util.Iterator;
//import java.util.Set;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author Kevlin
// * 
// */
//@Service
//public class RedisService{
//	
//	private static final Logger logger = LoggerFactory.getLogger(RedisService.class);
//	
//	@Autowired 
//	private StringRedisTemplate stringRedisTemplate;
//	@Autowired
//	private RedisTemplate redisTemplate;
//	
//	/**
//	 * 删除redis的记录 
//	 * redisKey :redis的关键字
//	 * likeMatch ：是否模糊删除 true模糊删除，false精确删除
//	 */
//	@SuppressWarnings("rawtypes")
//	public void deleteRedisRecords(String redisKeys,boolean likeMatch ){
//		if( likeMatch ){
//			Set<String> set = stringRedisTemplate.keys( redisKeys+"*" );
//			Iterator it = set.iterator();
//			while (it.hasNext()) {
//			   String redisKey = (String) it.next();
//			   logger.info("删除redis缓存:::"+redisKey);
//				stringRedisTemplate.delete(redisKey);
//			}
//		}else{
//			stringRedisTemplate.delete(redisKeys);
//			logger.info("删除redis缓存:::"+redisKeys);
//		}
//	}
//
//	/**
//	 * 设置key的过期时间
//	 * @param redisKey redis的key
//	 * @param timeLength 失效时长,单位秒
//	 * @return
//	 */
//	public void expire(final String redisKey,final long timeLength) {
//		stringRedisTemplate.execute(new RedisCallback<Object>() {
//            @Override
//            public Object doInRedis(RedisConnection connection) throws DataAccessException {
//            	byte [] key = stringRedisTemplate.getStringSerializer().serialize(redisKey);
//            	if(connection.exists(key)){
//            		connection.expire(key, timeLength);
//            		logger.info("设置redis缓存失效时间:::【"+redisKey+"】在"+timeLength+"后失效!");
//            	}
//                return null;
//            }
//        });
//	}
//    
//	/**
//	 * 检测是否已存在redis的key
//	 */
//	public boolean checkRedisKey( String redisKey ){
//		return stringRedisTemplate.hasKey(redisKey);
//	}
//	
//	/**
//	 * 获取redis的值
//	 */
//	public String getRedisValue( String redisKey ){
//		//不存在key
//		if( !checkRedisKey(redisKey) ){
//			return null;
//		}
//		return stringRedisTemplate.opsForValue().get(redisKey);
//	}
//	
//	/**
//	 * 设置redis的值，带失效时间
//	 * 单位 秒
//	 */
//	public void setRedisValue( String redisKey,String redisVal,int time ){
//		stringRedisTemplate.opsForValue().set(redisKey, redisVal, time, TimeUnit.SECONDS);
//	}
//	
//	/**
//	 * 设置redis的值，不失效
//	 */
//	public void setRedisValue( String redisKey,String redisVal ){
//		stringRedisTemplate.opsForValue().set(redisKey, redisVal);
//	}
//
//	//---------redis List-------------
//	//------操作方法 pop、push等--------
//
//	/**
//	 * 获取缓存对象
//	 * @param key 名称
//	 * @return
//	 */
//	public Object get(String key) {
//		String type = redisTemplate.boundListOps(key).getType().toString();
//		if ("LIST".equals(type)) {
//			return redisTemplate.boundListOps(key).range(0, -1);
//		}
//		if ("STRING".equals(type)) {
//			return redisTemplate.opsForValue().get(key);
//		}
//		if ("SET".equals(type)) {
//			return redisTemplate.boundSetOps(key).members();
//		}
//		if ("ZSET".equals(type)) {
//			return redisTemplate.boundZSetOps(key).range(0, -1);
//		}
//		if ("HASH".equals(type)) {
//			return redisTemplate.boundHashOps(key).entries();
//		}
//		if ("NONE".equals(type)) {
//			return "none：";
//		}
//		return null;
//	}
//
//	/**
//	 * List左插入数据
//	 * @param key
//	 * @param element
//	 */
//	public void setListLeftPush(String key,String element) {
//		redisTemplate.boundListOps(key).leftPush(element);
//	}
//
//	/**
//	 * List右插入数据
//	 * @param key
//	 * @param element
//	 */
//	public void setListRightPush(String key,String element) {
//		redisTemplate.boundListOps(key).rightPush(element);
//	}
//
//	/**
//	 * List左弹出数据
//	 * @param key
//	 */
//	public Object popListLeft(String key) {
//		return redisTemplate.boundListOps(key).leftPop();
//	}
//
//	/**
//	 * List右弹出数据
//	 * @param key
//	 */
//	public Object popListRight(String key) {
//		return redisTemplate.boundListOps(key).rightPop();
//	}
//
////	/**
////	 * 设置redis的值，带失效时间
////	 * 单位 秒
////	 */
////	public void setRedisListValue( String redisKey,List<T> list,int time ){
////		redisTemplate.opsForValue().set(redisKey,ListTranscoder.serialize(list),time, TimeUnit.SECONDS);
////	}
////	
////	/**
////	 * 获取list的值
////	 * @param redisKey
////	 * @return
////	 */
////	@SuppressWarnings("unchecked")
////	public List<T> getRedisListValue(String redisKey){
////		String str = getRedisValue(redisKey);
////		if( StringUtils.isEmpty(str) ){
////			return null;
////		}
////		List<T> list = (List<T>)ListTranscoder.deserialize(str.getBytes()); 
////		return list;
////	}
////	
////	 static class ListTranscoder{  
////	        public static String serialize(Object value) {  
////	            if (value == null) {  
////	                throw new NullPointerException("Can't serialize null");  
////	            }  
////	            byte[] rv=null;  
////	            ByteArrayOutputStream bos = null;  
////	            ObjectOutputStream os = null;  
////	            try {  
////	                bos = new ByteArrayOutputStream();  
////	                os = new ObjectOutputStream(bos);  
////	                os.writeObject(value);  
////	                os.close();  
////	                bos.close();  
////	                rv = bos.toByteArray();  
////	            } catch (IOException e) {  
////	                throw new IllegalArgumentException("Non-serializable object", e);  
////	            } finally {  
////	                close(os);  
////	                close(bos);  
////	            }  
////	            return rv.toString();  
////	        }  
////	  
////	        public static Object deserialize(byte[] in) {  
////	            Object rv=null;  
////	            ByteArrayInputStream bis = null;  
////	            ObjectInputStream is = null;  
////	            try {  
////	                if(in != null) {  
////	                    bis=new ByteArrayInputStream(in);  
////	                    is=new ObjectInputStream(bis);  
////	                    rv=is.readObject();  
////	                    is.close();  
////	                    bis.close();  
////	                }  
////	            } catch (IOException e) {  
////	                logger.warn("Caught IOException decoding %d bytes of data",  
////	                        in == null ? 0 : in.length, e);  
////	            } catch (ClassNotFoundException e) {  
////	                logger.warn("Caught CNFE decoding %d bytes of data",  
////	                        in == null ? 0 : in.length, e);  
////	            } finally {  
////	                close(is);  
////	                close(bis);  
////	            }  
////	            return rv;  
////	        }  
////	    } 
////	 
////	 static class ObjectsTranscoder{  
////         
////	        public static byte[] serialize(List<Object> value) {  
////	            if (value == null) {  
////	                throw new NullPointerException("Can't serialize null");  
////	            }  
////	            byte[] rv=null;  
////	            ByteArrayOutputStream bos = null;  
////	            ObjectOutputStream os = null;  
////	            try {  
////	                bos = new ByteArrayOutputStream();  
////	                os = new ObjectOutputStream(bos);  
////	                for(Object obj : value){  
////	                    os.writeObject(obj);  
////	                }  
////	                os.writeObject(null);  
////	                os.close();  
////	                bos.close();  
////	                rv = bos.toByteArray();  
////	            } catch (IOException e) {  
////	                throw new IllegalArgumentException("Non-serializable object", e);  
////	            } finally {  
////	                close(os);  
////	                close(bos);  
////	            }  
////	            return rv;  
////	        }  
////	  
////	        public static List<Object> deserialize(byte[] in) {  
////	            List<Object> list = new ArrayList<Object>();  
////	            ByteArrayInputStream bis = null;  
////	            ObjectInputStream is = null;  
////	            try {  
////	                if(in != null) {  
////	                    bis=new ByteArrayInputStream(in);  
////	                    is=new ObjectInputStream(bis);  
////	                    while (true) {  
////	                    	Object obj = (Object) is.readObject();  
////	                        if(obj == null){  
////	                            break;  
////	                        }else{  
////	                            list.add(obj);  
////	                        }  
////	                    }  
////	                    is.close();  
////	                    bis.close();  
////	                }  
////	            } catch (IOException e) {  
////	                logger.warn("Caught IOException decoding %d bytes of data",  
////	                        in == null ? 0 : in.length, e);  
////	            } catch (ClassNotFoundException e) {  
////	                logger.warn("Caught CNFE decoding %d bytes of data",  
////	                        in == null ? 0 : in.length, e);  
////	            } finally {  
////	                close(is);  
////	                close(bis);  
////	            }  
////	            return list;  
////	        }  
////	    }  
////	 
////	 public static void close(Closeable closeable) {  
////	        if (closeable != null) {  
////	            try {  
////	                closeable.close();  
////	            } catch (Exception e) {  
////	                logger.info("Unable to close %s", closeable, e);  
////	            }  
////	        }  
////	    }
//}
